from atm_oop.utility_class import Utility
from atm_oop.user_class import User
import time

class Account:
    def __init__(self):
        self.utility = Utility()
        self.menu = None
    # 查询
    def query(self):
        from atm_oop.menu_class import Menu
        self.menu = Menu()
        login_name = User.login_name
        result = self.utility.query_one("select * from user_info where username = '%s'" %login_name)
        output = result[0] + "\n" + "手机号："+ result[2] + "\n" + "余额："+ str(result[3])
        print(output)
        self.menu.main_menu()

    #  账户金额的操作
    def change_balance(self,username, money):
        balance = self.utility.query_one("select balance from user_info where username = '%s'" %username)
        new_balance = balance[0] + float(money)
        self.utility.db_operation("update user_info set balance = %f where username = '%s'" % (new_balance, username))
        if float(money) > 0:
            print("成功存款%f，剩余金额%f" % (money, new_balance))
        else:
            print("成功取款%f，剩余金额%f" % (money * -1, new_balance))

    # 取款
    def withdraw(self):
        from atm_oop.menu_class import Menu
        self.menu = Menu()
        login_name = User.login_name
        money = input("请输入你的取款金额：")
        balance = self.utility.query_one("select balance from user_info where username = '%s'" %login_name)
        if self.utility.check_number(money) and float(money) <= balance[0] and float(money) % 100 == 0:
            self.change_balance(login_name,float(money) * -1)
            string = time.strftime('%Y-%m-%d %H:%M:%S')+" "+"取款"+money
            self.utility.db_operation("insert into history1(username,content)values('%s','%s')" %(login_name,string))
            self.menu.main_menu()
        else:
            print("取款金额有误")
            self.withdraw()

    # 存款
    def deposite(self):
        from atm_oop.menu_class import Menu
        self.menu = Menu()
        login_name = User.login_name
        money = input("请输入你的存款金额：")
        if self.utility.check_number(money) and float(money) % 100 == 0:
            self.change_balance(login_name, float(money))
            string = time.strftime('%Y-%m-%d %H:%M:%S') + " " + "存款" + money
            self.utility.db_operation("insert into history1(username,content)values('%s','%s')" % (login_name, string))
            self.menu.main_menu()
        else:
            print("存款金额有误")
            self.deposite()

    # 转账
    def transer(self):
        from atm_oop.menu_class import Menu
        self.menu = Menu()
        login_name = User.login_name
        account = input("请输入你的转账账号：")
        account_search = self.utility.query_one("select username from user_info where username = '%s'" %account)
        if account_search != None and account != login_name:
            money = input("请输入你的转账金额：")
            if self.utility.check_number(money):
                money = float(money)
                balance1 = self.utility.query_one("select balance from user_info where username = '%s'" %login_name)
                if money <= balance1[0]:
                    new_balance1 = balance1[0] - float(money)
                    self.utility.db_operation("update user_info set balance = %f where username = '%s'" % (new_balance1, login_name))
                    print("成功转账%f！当前余额为：%f" %(money,new_balance1))
                    balance2 = self.utility.query_one("select balance from user_info where username = '%s'" %account)
                    new_balance2 = balance2[0] + float(money)
                    self.utility.db_operation("update user_info set balance = %f where username = '%s'" % (new_balance2, account))
                    print("另一个账户余额为：%f" %new_balance2)  # 方便检查另一个账户是否有进账
                    string1 = time.strftime('%Y-%m-%d %H:%M:%S') + " " + "转出" + str(money)
                    self.utility.db_operation("insert into history1(username,content)values('%s','%s')" % (login_name, string1))
                    string2 = time.strftime('%Y-%m-%d %H:%M:%S') + " " + "转入" + str(money)
                    self.utility.db_operation("insert into history1(username,content)values('%s','%s')" % (account, string2))
                else:
                    print("余额不足")
                    self.transer()
            else:
                print("你输入的金额不符合规范")
                self.transer()
        elif account == login_name:
            print("你输入的账号为当前账号")
            self.transer()
        else:
            print("你输入的账号不存在")
            self.transer()
        self.menu.main_menu()

    # 流水
    def history(self):
        from atm_oop.menu_class import Menu
        self.menu = Menu()
        login_name = User.login_name
        result = self.utility.query_all("select username,content from history1 where username = '%s'" %login_name)
        if result ==():
            print('当前用户流水信息为空')
        else:
            for h in result:
                print(h[0] + ' ' + h[1])
        self.menu.main_menu()
